﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using DBAccessLib;
using System.IO;

namespace LineWatch_Web
{
    public partial class ConfigurationManagement : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["UserName"] == null || Session["UserName"].ToString().Equals(""))
                {
                    Response.Redirect("ProductManagement.aspx");
                }
                if (!(bool)Session["HasRight"])
                {
                    Response.Redirect("ProductManagement.aspx");
                }
                BindLine();
                BindData();
            }
        }

        private void BindData()
        {
            DataTable dt = Config.GetLineWatchSys(ddlLine.SelectedItem.Value);
            if (dt == null) return;
            DataRow[] drs = dt.Select("FUNCTION = 'ShiftA'");
            if (drs.Length > 0)
            {
                txtShiftA1.Text = drs[0]["CONTENT1"].ToString().Trim();
                txtShiftA2.Text = drs[0]["CONTENT2"].ToString().Trim();
            }
            else
            {
                txtShiftA1.Text = "";
                txtShiftA2.Text = "";
            }

            drs = dt.Select("FUNCTION = 'ShiftB'");
            if (drs.Length > 0)
            {
                txtShiftB1.Text = drs[0]["CONTENT1"].ToString().Trim();
                txtShiftB2.Text = drs[0]["CONTENT2"].ToString().Trim();
            }
            else
            {
                txtShiftB1.Text = "";
                txtShiftB2.Text = "";
            }

            drs = dt.Select("FUNCTION = 'ScreenP'");
            if (drs.Length > 0)
            {
                labPic1.Text = drs[0]["CONTENT1"].ToString().Trim();
                labPic2.Text = drs[0]["CONTENT2"].ToString().Trim();
                labPic3.Text = drs[0]["CONTENT3"].ToString().Trim();
                labPic4.Text = drs[0]["CONTENT4"].ToString().Trim();
            }
            else
            {
                labPic1.Text = "";
                labPic2.Text = "";
                labPic3.Text = "";
                labPic4.Text = "";
            }

            drs = dt.Select("FUNCTION = 'ScreenW'");
            if (drs.Length > 0)
            {
                txtWord.Text = drs[0]["CONTENT1"].ToString().Trim();
                string tmp = drs[0]["CONTENT2"].ToString().Trim();
                if (!tmp.Trim().Equals(""))
                {
                    ddlFont.SelectedIndex = ddlFont.Items.IndexOf(ddlFont.Items.FindByValue(tmp));
                }
                tmp = drs[0]["CONTENT3"].ToString().Trim();
                if (!tmp.Trim().Equals(""))
                {
                    ddlSize.SelectedIndex = ddlSize.Items.IndexOf(ddlSize.Items.FindByValue(tmp));
                }
                tmp = drs[0]["CONTENT4"].ToString().Trim();
                if (!tmp.Trim().Equals(""))
                {
                    ddlColor.SelectedIndex = ddlColor.Items.IndexOf(ddlColor.Items.FindByValue(tmp));
                }
            }
            else
            {
                txtWord.Text = "";
            }

            drs = dt.Select("FUNCTION = 'MusicH'");
            if (drs.Length > 0)
            {
                labMusicH.Text = drs[0]["CONTENT1"].ToString().Trim();
            }
            else
            {
                labMusicH.Text = "";
            }

            drs = dt.Select("FUNCTION = 'MusicS'");
            if (drs.Length > 0)
            {
                labMusicS.Text = drs[0]["CONTENT1"].ToString().Trim();
            }
            else
            {
                labMusicS.Text = "";
            }

            drs = dt.Select("FUNCTION = 'MusicA'");
            if (drs.Length > 0)
            {
                labMusicA.Text = drs[0]["CONTENT1"].ToString().Trim();
            }
            else
            {
                labMusicA.Text = "";
            }
        }

        private void BindLine()
        {
            DataTable dt = Product.GetAllLines();
            ddlLine.Items.Clear();
            foreach (DataRow row in dt.Rows)
            {
                ListItem li = new ListItem(row["Line_Name"].ToString().Trim(), row["Line_Code"].ToString().Trim());
                ddlLine.Items.Add(li);
            }
        }

        protected void btnPic_Click(object sender, EventArgs e)
        {
            UploadPic(fuPic1, labPic1);
            UploadPic(fuPic2, labPic2);
            UploadPic(fuPic3, labPic3);
            UploadPic(fuPic4, labPic4);
        }

        private void UploadPic(FileUpload fuPic, Label labfilename)
        {
            if (fuPic.FileName.Length > 0)
            {
                FileInfo info = new FileInfo(fuPic.FileName);
                if (info.Extension.Equals(".jpg") || info.Extension.Equals("jpeg") || info.Extension.Equals("bmp") || info.Extension.Equals("gif"))
                {
                    string folderPath = Server.MapPath("") + "/../Files/Upload/" + ddlLine.SelectedValue;
                    if (!Directory.Exists(folderPath)) Directory.CreateDirectory(folderPath);
                    fuPic.SaveAs(String.Format(@"{0}\{1}\{2}", Server.MapPath("") + "/../Files/Upload", ddlLine.SelectedValue, fuPic.FileName));
                    labfilename.Text = fuPic.FileName;
                }
                else
                {
                    Response.Write("<script language=javascript> alert('上传失败!\\n\\n请选择以 .jpg .jepg .bmp .gif 为后缀的文件') </script>");
                }
            }
        }

        protected void btnMusic_Click(object sender, EventArgs e)
        {
            UploadMusic(fuMusicHelp, labMusicH);
            UploadMusic(fuMusicStop, labMusicS);
            UploadMusic(fuMusicAlarm, labMusicA);
        }

        private void UploadMusic(FileUpload fuPic, Label labfilename)
        {
            if (fuPic.FileName.Length > 0)
            {
                FileInfo info = new FileInfo(fuPic.FileName);
                if (info.Extension.Equals(".mp3") || info.Extension.Equals(".wav") || info.Extension.Equals(".mid"))
                {
                    string folderPath = Server.MapPath("") + "/../Files/Upload/" + ddlLine.SelectedValue;
                    if (!Directory.Exists(folderPath)) Directory.CreateDirectory(folderPath);
                    fuPic.SaveAs(String.Format(@"{0}\{1}\{2}", Server.MapPath("") + "../Files/Upload", ddlLine.SelectedValue, fuPic.FileName));
                    labfilename.Text = fuPic.FileName;
                }
                else
                {
                    Response.Write("<script language=javascript> alert('上传失败!\\n\\n请选择以 .mp3 .wav .mid 为后缀的文件') </script>");
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            SaveData("ScreenP", ddlLine.SelectedItem.Value, labPic1.Text, labPic2.Text, labPic3.Text, labPic4.Text, Session["UserName"].ToString());

            SaveData("ScreenW", ddlLine.SelectedItem.Value, txtWord.Text, ddlFont.SelectedValue, ddlSize.SelectedValue, ddlColor.SelectedValue, Session["UserName"].ToString());

            SaveData("MusicH", ddlLine.SelectedItem.Value, labMusicH.Text, "", "", "", Session["UserName"].ToString());

            SaveData("MusicS", ddlLine.SelectedItem.Value, labMusicS.Text, "", "", "", Session["UserName"].ToString());

            SaveData("MusicA", ddlLine.SelectedItem.Value, labMusicA.Text, "", "", "", Session["UserName"].ToString());

            SaveData("ShiftA", ddlLine.SelectedItem.Value, txtShiftA1.Text, txtShiftA2.Text, "", "", Session["UserName"].ToString());

            SaveData("ShiftB", ddlLine.SelectedItem.Value, txtShiftB1.Text, txtShiftB2.Text, "", "", Session["UserName"].ToString());
        }

        private void SaveData(string function, string line, string content1, string content2, string content3, string content4, string user)
        {
            Config.UpdateLineWatchSysRecord(function, line, content1, content2, content3, content4, user);
        }

        protected void ddlLine_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindData();
        }
    }
}